package login;


import java.io.IOException;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionBean.LoginCtrlRemote;

import entityBean.Agenzia;
import entityBean.Cliente;
import entityBean.Operatore;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			
			processaRichiesta(request, response);
			
		} catch (NamingException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		try {
			
			processaRichiesta(request, response);
			
		} catch (NamingException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	
	}
	
	private LoginCtrlRemote getLoginCtrl() throws NamingException {
		
		Properties p = new Properties();
		p.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
		p.setProperty("java.naming.provider.url", "localhost:1099");
		p.setProperty("java.naming.url.pkgs", "org.jboss.naming");
		
		Object obj = new InitialContext(p).lookup("LoginCtrl/remote");
		
		return (LoginCtrlRemote)PortableRemoteObject.narrow(obj, LoginCtrlRemote.class);
		
	}
	
	public void processaRichiesta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NamingException {
		
		LoginCtrlRemote login = getLoginCtrl();  //recupero il session bean
		
		String user = request.getParameter("user");
		String username= request.getParameter("username");
		String password = request.getParameter("password");
		
		if(user.equals("operatore")) {
			
			Operatore o = login.loginOperatore(username, password);
			
			if(o == null) {
				
				//ho trovato più operatori uguali
				
			}
			
			else if(o.getCodOp().equals("NonTrovato")) {
				
				//passare il controllo alla jsp HomeErr
				
			}
			
			else {
				
				//passare il controllo alla jsp Operatore
				
			}
			
		}
		
		else if(user.equals("agenzia")) {
			
			Agenzia a = login.loginAgenzia(username, password);
			
			if(a == null) {
				
				
				
			}
			
			else if(a.getUsername().equals("NonTrovato")) {
				
				//passare il controllo alla jsp HomeErr
				
			}
			
			else {
				
				//passare il controllo alla pagina Agenzia
				
			}
			
		}
		
		else {
			
			Cliente c = login.loginCliente(username, password);
			
			if(c == null) {
				
				
				
			}
			
			else if(c.getUsername().equals("NonTrovato")) {
				
				//passare controllo alla pagina HomeErr
				
			}
			
			else {
				
				//passare il controllo alla pagina Cliente
				
			}
			
		}
		
	}

}
